package com.edgelight.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class LoggerAop {


    private static final Logger logger= LoggerFactory.getLogger(LoggerAop.class);

    //定义切点 execution 切点表达式
    @Pointcut(value = "execution(* *.*.service.*.*(..))")
    public void pt1(){}

    @Around(value = "pt1()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        //1.记录执行的时间，以及执行的方法
        MethodSignature method = (MethodSignature)proceedingJoinPoint.getSignature();
        logger.debug("================当前时间："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"进入了方法："+method.getMethod().getName()+"===========");
        Object proceed=null;
        try {
            proceed = proceedingJoinPoint.proceed();
            logger.debug("==========="+method.getMethod().getName()+"执行完成===============");
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("==========="+method.getMethod().getName()+"异常：{}"+throwable.getMessage());
        } finally {
            logger.debug("方法执行结束："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        return proceed;
    }
}
